package com.jhd.fuzhou.view.monitoring;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;

import com.github.mikephil.charting.charts.LineChart;
import com.jhd.fuzhou.R;
import com.jhd.fuzhou.linechart.DrawBgType;
import com.jhd.fuzhou.linechart.DrawConnectLineType;
import com.jhd.fuzhou.linechart.DrawLineType;
import com.jhd.fuzhou.linechart.LineChartView;
import com.jhd.fuzhou.util.CompareFloat;
import com.jhd.fuzhou.util.DeleteUnuseZero;
import com.jhd.fuzhou.util.MPLineChartManager;
import com.jhd.fuzhou.view.login.FuzhouApplication;
import com.jhd.fuzhou.vo.AlarmDataVo;
import com.jhd.fuzhou.vo.ChartBean;
import com.jhd.fuzhou.vo.CommonParamLimitVo;

import net.zigin.commons.util.StringUtil;

import java.math.BigDecimal;
import java.util.ArrayList;

public class MonitorFragment extends Fragment {
    private final FuzhouApplication application;
    private Context context;
    private AlarmDataVo alarmDataVo;private CommonParamLimitVo commonParamLimitVo;
    private TextView tv_originValue,tv_unit,tv_status,tv_time,tv_standard_value,tv_alarmTypeItem,tv_supervisedUnitName,tv_managementAddress;
    private LineChart lineChart;
    private ProgressDialog _dialog;
    private String[] xList;private float[] yList;
    private MPLineChartManager mpLineChartManager;
    private LineChartView lineChartView;
    private ArrayList<ChartBean> lineChartBeanList;private View rootView;

    public MonitorFragment(FuzhouApplication application, Context context,AlarmDataVo alarmDataVo,String[] xList,float[] yList, CommonParamLimitVo commonParamLimitVo) {
        this.alarmDataVo=alarmDataVo;
        this.context=context;
        this.application=application;
        this.commonParamLimitVo=commonParamLimitVo;
        this.xList=xList;
        this.yList=yList;
    }


    private void setDate(AlarmDataVo alarmDataVo,String[] xList,float[] yList, CommonParamLimitVo commonParamLimitVo) {
        float max=getMax();
        if(!CompareFloat.compareFloat(commonParamLimitVo.getNormalLowLimit(),commonParamLimitVo.getNormalHighLimit(),max)){//超标
            tv_originValue.setTextColor(getActivity().getResources().getColor(R.color.red));
            tv_status.setBackground(getActivity().getResources().getDrawable(R.drawable.above_background));
            tv_status.setText("超标");
        }else{
            tv_status.setText("正常");
        }
        tv_originValue.setText(DeleteUnuseZero.convertByPattern(String.valueOf(max)));
        tv_unit.setText(StringUtil.isNullOrEmpty(commonParamLimitVo.getItemUnit())?"":commonParamLimitVo.getItemUnit());
        tv_time.setText(StringUtil.isNullOrEmpty(alarmDataVo.getCreateTime())?"":alarmDataVo.getCreateTime());
        tv_standard_value.setText(""+commonParamLimitVo.getNormalLowLimit()+"~"+commonParamLimitVo.getNormalHighLimit()+commonParamLimitVo.getItemUnit());
        tv_alarmTypeItem.setText(StringUtil.isNullOrEmpty(alarmDataVo.getAlarmTypeItem())?"":alarmDataVo.getAlarmTypeItem());
        tv_supervisedUnitName.setText(StringUtil.isNullOrEmpty(alarmDataVo.getSupervisedUnitName())?"":alarmDataVo.getSupervisedUnitName());
        tv_managementAddress.setText(StringUtil.isNullOrEmpty(alarmDataVo.getManagementAddress())?"":alarmDataVo.getManagementAddress());
        clickLineChart(xList, yList, commonParamLimitVo);
    }
    private float getMax(){
        int m=1;float max=0;
        if(xList.length>6){
            m=xList.length/6;
        }
        for(int i=0;i<(xList.length>6?6:xList.length);i++){
            BigDecimal a = new BigDecimal(yList[i*m]);
            BigDecimal b = new BigDecimal(max);
            if(a.compareTo(b)>0){
                max=yList[i*m];
            }
        }
        return max;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
        if (null == rootView) {
            rootView = inflater.inflate(R.layout.fragment_monitor, null);
            tv_originValue=(TextView)rootView.findViewById(R.id.tv_originValue);
            tv_unit=(TextView)rootView.findViewById(R.id.tv_unit);
            tv_status=(TextView)rootView.findViewById(R.id.tv_status);
            tv_time=(TextView)rootView.findViewById(R.id.tv_time);
            tv_standard_value=(TextView)rootView.findViewById(R.id.tv_standard_value);
            tv_alarmTypeItem=(TextView)rootView.findViewById(R.id.tv_alarmTypeItem);
            tv_supervisedUnitName=(TextView)rootView.findViewById(R.id.tv_supervisedUnitName);
            tv_managementAddress=(TextView)rootView.findViewById(R.id.tv_managementAddress);
            lineChartView=(LineChartView)rootView.findViewById(R.id.line_chart_view);
            //lineChart=(LineChart)view.findViewById(R.id.lineChart);
            setDate(alarmDataVo,xList,yList,commonParamLimitVo);// 控件初始化
        }
        return rootView;
    }
    private void clickLineChart(String[] xList,float[] yList, CommonParamLimitVo commonParamLimitVo) {
        /*mpLineChartManager = new MPLineChartManager(lineChart);
        mpLineChartManager.setXAxis(true, true, 10f, Color.BLUE, XAxis.XAxisPosition.BOTTOM, 10f);
        //假数据 折线1111
        ArrayList<Entry> values1 = new ArrayList<Entry>();
        *//*for(int i=0;i<xList.length;i++){
            values1.add(new Entry(xList[i],yList[i]));
        }*//*
        values1.add(new Entry(100, 120));
        values1.add(new Entry(120, 150));
        values1.add(new Entry(130, 120));
        values1.add(new Entry(140, 130));
        values1.add(new Entry(150, 110));
        mpLineChartManager.addData(values1,StringUtil.isNullOrEmpty(alarmDataVo.getItemUnit())?"":alarmDataVo.getItemUnit(), 0f, Color.BLUE,8f,
                false, 5f, Color.GREEN,
                true, Color.YELLOW);

        mpLineChartManager.setData();
        mpLineChartManager.setInteraction(true,true, true, true, false,
                false, false, false);
        mpLineChartManager.setLegend(Legend.LegendPosition.ABOVE_CHART_LEFT, 10f, Color.BLACK, Legend.LegendForm.CIRCLE);
        mpLineChartManager.invalidate();*/
        if(lineChartBeanList ==null){
            lineChartBeanList = new ArrayList<>();
        }else{
            lineChartBeanList.clear();
        }
        lineChartView.setDefaultTextSize(24);
        int m=1;
        if(xList.length>6){
            m=xList.length/6;
        }
        for(int i=0;i<xList.length;i++){
            if(i%m==0){
                ChartBean lineChartBean = new ChartBean();
                lineChartBean.setValue(String.valueOf(yList[i]));
                lineChartBean.setDate(xList[i].substring(11,16));
                lineChartBeanList.add(lineChartBean);
            }
        }
        lineChartView.setData(lineChartBeanList);
        lineChartView.setRange(commonParamLimitVo.getNormalLowLimit(),commonParamLimitVo.getNormalHighLimit());
        lineChartView.setyLableText("单位:"+commonParamLimitVo.getItemUnit());
        //设置点击背景（可以为图片，也可以为一个颜色背景，大小根据textAndClickBgMargin设置）
        lineChartView.setDrawBgType(DrawBgType.DrawBackground);
        //设置图片资源
        lineChartView.setShowPicResource(R.drawable.add_background);
        //连接线为虚线（也可以为实现）
        lineChartView.setDrawConnectLineType(DrawConnectLineType.DrawDottedLine);
        lineChartView.setClickable(false);
        //是否需要画连接线
        lineChartView.setNeedDrawConnectYDataLine(true);
        //连接线的颜色
        lineChartView.setConnectLineColor(getResources().getColor(R.color.default_color));
        //是否需要背景
        lineChartView.setNeedBg(true);
        //画曲线图（也可以为折线图）
        lineChartView.setDrawLineType(DrawLineType.Draw_Line);
    }


}